package com.android.fragmentdemo.fragments
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map
import androidx.lifecycle.switchMap
import com.hengfoxwolf.commonbase.base.databeans.ItemData

class MineViewModel : ViewModel() {

    private val mItemData = ItemData("haha--gogo","this is a haha--gogo")

    private var mLiveData = MutableLiveData<Int>()

    val mLiveDataMap : LiveData<String> = mLiveData.map {
        "$it---onChange/${mItemData.title}--${mItemData.detailDesc}"
    }

    private val mLiveData0 = MutableLiveData<String>()

    private val mLiveData1 = MutableLiveData<String>()

    private val mLiveDataSwitch = MutableLiveData<Boolean>()

    val mLiveDataSwitchMap : LiveData<String> = mLiveDataSwitch.switchMap {
        if (it) {
            mLiveData0
        } else {
            mLiveData1
        }
    }


    private val mLiveData3 = MutableLiveData<ItemData>()

    private val mLiveData4 = MutableLiveData<ItemData>()

    val mMediatorLiveData = MediatorLiveData<ItemData>().apply {
        addSource(mLiveData3) {
            value = it
        }

        addSource(mLiveData4) {
            value = it
        }
    }



    fun updateValue(value: Int) {
        mLiveData.value = value
    }

    fun updateValue0(value0: String) {
        mLiveData0.value = value0
    }

    fun updateValue1(value1: String) {
        mLiveData1.value = value1
    }

    fun updateSwitchValue(switchValue: Boolean) {
        mLiveDataSwitch.value = switchValue
    }

    fun updateMediatorValue(title: String, detail: String) {
        mLiveData3.value = ItemData(title, detail)
    }
}